package org.jeecg.modules.business.mapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.business.dto.KedanOrderDto;
import org.jeecg.modules.business.dto.TodayBookingQueryDto;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusRoomBookingOrders;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.business.entity.BusRoomPayType;
import org.jeecg.modules.business.entity.BusRoomsLivingOrder;
import org.jeecg.modules.business.vo.*;
import org.jeecg.modules.rooms.Vo.LivingContinueVo;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.springframework.format.annotation.DateTimeFormat;

import javax.xml.crypto.Data;

/**
 * @Description: 酒店预定订单
 * @Author: jeecg-boot
 * @Date:   2023-03-25
 * @Version: V1.0
 */
public interface BusRoomBookingOrdersMapper extends BaseMapper<BusRoomBookingOrders> {
    List<TodayBookingQueryDto> getTodayBookingOrder(@Param("roomIds") List<String> roomIds);

    List<BusBookingRooms> conflictBookingTime(
            @Param("startAt") Date startAt,
            @Param("endAt") Date endAt,
            @Param("roomIds") List<String> roomIds);

    IPage<RoomLivingQueryVo> getPage(IPage<RoomLivingQueryVo> page,@Param("status") Integer status,@Param("keyw") String keyw,@Param("hotelId") String hotelId);


    IPage<RoomLivingQueryVo> getUnionPage(IPage<RoomLivingQueryVo> page, @Param("keyw") String keyw,@Param("hotelId") String hotelId ,@Param("isMain") Boolean isMain,@Param("bookingIds") List<String> bookingIds);

    List<ForwardFangTaiQueryVo> forwardFangTaiQuery(String hotelId,
                                                    String start,
                                                    String end);
    List<LayoutRoomCountVo> getLayoutRoomCount(String hotelId);

    List<RiLiFangTaiVo> getRiliFangTai(String start, String end,String hotelId);

    IPage<KeLiItemVo> getKeLiLiving(IPage<KeLiItemVo> page, KedanOrderDto kedanOrderDto);
    IPage<KeLiItemVo> getKeBooking(Page<KeLiItemVo> page, KedanOrderDto kedanOrderDto);
    @Select("SELECT o.id from bus_room_booking_orders o inner join bus_booking_rooms br on o.id = br.booking_orders_id inner join ces_room_layout l on l.id = br.room_layout_id where l.hotel_id = #{hotelId} and o.arrival_time >= now() and o.arrival_time <= DATE_ADD(o.arrival_time,interval 1 day)")
    List<BusRoomBookingOrders> countTodayYD(String hotelId);


    /**
     * 根据手机号查询入住历史列表
     * @param page
     * @param hotelId
     * @param phone
     * @return
     */
    @Select("<script>select o.due_out_time,r.name as room_name,layout.name as layout_name,ldp.price,bc.id as customer_id, bc.black_status, bc.`name`,bc.phone,bc.cert_no,bc.address,bc.nation, case ifnull(card.id,'0') when '0' then '否' else '是' end as huiyuan " +
            "from bus_rooms_living_order o " +
            "inner join bus_customer bc on bc.id=o.contact_id\n" +
            "left join bus_booking_rooms br on br.id=o.booking_room_id\n" +
            "left join ces_rooms r on r.id=br.room_id\n" +
            "left join ces_room_layout layout on layout.id=r.layout_id\n" +
            "left join (select living_order_id,max(price) as price from bus_living_layout_day_price group by living_order_id) ldp on ldp.living_order_id=o.id\n" +
            "left join bus_member_card card on card.mobile=bc.phone\n"+
            "where 1=1" +
            "<if test='hotelId != null and hotelId !=\"\"'> and o.hotel_id = '${hotelId}' </if>" +
            "<if test='phone != null and phone !=\"\"'> and bc.phone = '${phone}' </if>" +
            "</script>")
    public List<HashMap<String, Object>> ruZhuHistoryList(Page<HashMap<String, Object>> page, @Param("hotelId") String hotelId, @Param("phone") String phone);

    @Select("select brbo.id as booking_order_id, brbo.arrival_time,cr.name as room_name,cr.id as room_id, " +
            "bc.id as customer_id, bc.name as customer_name, bc.phone " +
            "from bus_room_booking_orders brbo " +
            "inner join bus_booking_rooms bbr on bbr.booking_orders_id = brbo.id " +
            "left join bus_customer bc on bc.id = brbo.contact_id " +
            "inner join ces_rooms cr on cr.id = bbr.room_id " +
            "where brbo.arrival_time > #{nowDate} && brbo.arrival_time < #{dueOutTime} " +
            "and brbo.booking_status = 1 "+
            "and bbr.room_id in " +
            "(select cr.id from bus_booking_rooms bbr " +
            "inner join ces_rooms cr on cr.id = bbr.room_id " +
            "where bbr.id = #{bookingRoomId}) ")
    List<LivingContinueVo> findBookingOrderByRoom(String bookingRoomId, Date nowDate, Date dueOutTime);

    @Select("<script>SELECT brbo.id,cr.name,brbo.arrival_time, brbo.due_out_time  from bus_booking_rooms bbr \n" +
            "left join ces_rooms cr on cr.id = bbr.room_id \n" +
            "left Join bus_room_booking_orders brbo  on bbr.booking_orders_id = brbo.id\n" +
            "where \n" +
            "brbo.booking_status = 1 and bbr.room_status != 4\n" +
            "AND brbo.arrival_time &lt;= #{endOf} AND brbo.due_out_time &gt;= #{startOf} \n" +
            "and cr.id in " +
            "<foreach collection='roomIds' item='id' open='(' separator=',' close=')'> " +
            "#{id} " +
            "</foreach> " +
            "</script>")
    List<BusRoomBookingOrders> findBookingOrder(List<String> roomIds, Date startOf, Date endOf);

    @Select("select brlo.*, cr.name as room_name,cr.id as room_id, bc.phone, bc.name as customer_name, cr.layout_id \n" +
            "from bus_rooms_living_order brlo \n" +
            "inner join ces_rooms cr on cr.living_order_id = brlo.id \n" +
            "left join bus_customer bc on brlo.contact_id = bc.id " +
            "where brlo.booking_order_id = #{ bookingOrderId } and brlo.hotel_id = #{ hotelId }")
    List<LivingContinueVo> getContinueOrder(String bookingOrderId, String hotelId);
}
